Skip to content

test(encoding): add GTest unit tests for detectCharset ESC sequence parsing - #760

Draft
pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:atut-supplement/deepin-editor-20261130
Draft

pengfeixx wants to merge 1 commit into
linuxdeepin:masterfrom
pengfeixx:atut-supplement/deepin-editor-20261130

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

补充 detectCharset 单元测试

关联提交: 4a16a5d fix(encodes): fix heap buffer overflow read in detectCharset (CWE-125)

测试覆盖

新增 tests/common/test_encoding.cpp,22 个 GTest 用例覆盖 detectCharset 全部分支:

分支 输入 期望结果
UTF-8 多字节 \xC3\xA9 (é) UTF-8
UTF-8 3字节 \xE4\xB8\xAD (中) UTF-8
ESC $ B JIS X 0208-1983 ISO-2022-JP
ESC $ @ JIS X 0208-1978 ISO-2022-JP
ESC $ A GB2312-1980 ISO-2022-JP-2
ESC $ ( C KSC5601-1987 ISO-2022-JP-2
ESC $ ( D JIS X 0212-1990 ISO-2022-JP-2
ESC $ ) C KSC5601-1987 ISO-2022-KR
ESC 后非 $ 继续扫描 default charset
ESC $ 未知字符 break default charset
ESC $ ( 未知字符 break default charset
ESC $ ) 非 C charset 空 default charset
NUL 终止 NUL 字节 default charset
纯 ASCII 无高字节/ESC default charset
空数组 边界回归 不崩溃
仅 ESC 边界回归 不崩溃
ESC $ 边界回归 不崩溃
ESC $ ( 边界回归 不崩溃
ESC $ ) 边界回归 不崩溃

本地验证

  • 编译:standalone CMake (Qt6::Core + GTest) ✅
  • 运行:22/22 PASSED ✅
  • 环境:zh_CN.UTF-8

变更文件

  • tests/common/test_encoding.cpp (新增)
  • tests/common/CMakeLists.txt (APPEND test_encoding target)

Summary by Sourcery

Add comprehensive unit tests for charset detection, including ESC sequence parsing and truncated-input safety regressions.

Enhancements:

  • Add comprehensive GTest coverage for charset detection across UTF-8, ESC-sequence, ASCII, NUL-terminated, invalid-input, and fallback cases.
  • Add regression tests for truncated ESC sequences to ensure charset detection handles boundary inputs without crashing.

Build:

  • Register a standalone encoding unit-test target linked with Qt Core.

Tests:

  • Add 22 unit tests covering detectCharset branches and edge cases, including the heap-buffer-overflow regression scenarios.

…arsing

Add test_encoding target in tests/common/ covering all detectCharset branches:
- UTF-8 valid input with multibyte characters → UTF-8
- ESC $ B / ESC $ @ → ISO-2022-JP (JIS X 0208)
- ESC $ A → ISO-2022-JP-2 (GB2312)
- ESC $ ( C / ESC $ ( D → ISO-2022-JP-2 (KSC5601 / JIS X 0212)
- ESC $ ) C → ISO-2022-KR (KSC5601)
- ESC followed by non-$ → continues scanning
- ESC $ followed by unknown char → falls back to default charset
- NUL byte terminates scanning
- Boundary conditions (commit 4a16a5d regression): empty array,
  truncated ESC / ESC $ / ESC $ ( / ESC $ ) sequences — verifies
  no heap buffer overread (CWE-125)

22 test cases, all passing.

Log: 补充 detectCharset ESC 序列解析及边界条件的 GTest 单元测试
@deepin-ci-robot

Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pengfeixx

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Reviewer's Guide

Tests

Add GTest coverage for detectCharset’s UTF-8 and ESC-sequence branches, fallback behavior, and truncated-input boundaries, and register the test target so these checks can be built and run.

  • tests/common/CMakeLists.txt L128-139
  • tests/common/test_encoding.cpp L1-205

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-bot

deepin-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.13
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants